//
//  WLOAuthViewController.swift
//  weibo
//
//  Created by 刁 海瑞 on 16/8/9.
//  Copyright © 2016年 毋磊. All rights reserved.
//

import UIKit
import SVProgressHUD
import AFNetworking

let WeiboAppKey = "2108008143"
let WeiboAppSecret = "924954e07aaae33e3dad82353a24780d"
let WeiboAppRedirect_Uri = "http://www.itcast.cn"
class WLOAuthViewController: UIViewController {
    
//    MAKE: --懒加载
    
    
    private lazy var webView: UIWebView = UIWebView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
       setNavUI()
        requestOAuthLogin()
    }
    
    override func loadView() {
        super.loadView()
        webView.opaque = false
        webView.delegate = self
        view = webView
        
    }
    
    private func setNavUI() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", tarfet: self, action: "cancelAction")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", tarfet: self, action: "autoFillAction")
        navigationItem.title = "微博登录"
        
    }
//    取消按钮点击事件
    @objc private func cancelAction() {
        dismissViewControllerAnimated(true, completion: nil)
        
    }
//    自动填充点击事件
    @objc private func autoFillAction() {
        webView.stringByEvaluatingJavaScriptFromString("document.getElementById('userId').value='524273836@qq.com';document.getElementById('passwd').value = 'wl9009240019'")
    }
    
//    请求登录第三方页面
    private func requestOAuthLogin() {
        
        let url = "https://api.weibo.com/oauth2/authorize?client_id=\(WeiboAppKey)&redirect_uri=\(WeiboAppRedirect_Uri)"
        
//        准备Redirect_Uri
        let urlRedirect = NSURLRequest(URL: NSURL(string: url)!)
        
        webView.loadRequest(urlRedirect)
    }
    
}
extension WLOAuthViewController: UIWebViewDelegate{
    
//    将要加载
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        print(request.URL?.absoluteString)
        
        guard let url = request.URL else{
            return false
        }
        
        if !url.absoluteString.hasPrefix(WeiboAppRedirect_Uri){
            
            return true
        }
        
        if let query = url.query where query.hasPrefix("code="){
            
            print(query)
            let code = query.substringFromIndex("code=".endIndex)
            print(code)
            WLUserAccountViewModel.sharedUserAccount.reqeustAccessTokenWithCode(code, callBack: { (isSuccess) -> () in
                if isSuccess {
                    
                    print("成功")
                }else{
                    
                    print("失败")
                }
            })
            
        }else{
            dismissViewControllerAnimated(true, completion: nil)
        }
        
        return false
        
    }
    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
        SVProgressHUD.dismiss()
    }
    
}


